package com.ruiya.staging.goods.cas.controller;

import com.ruiya.staging.goods.cas.model.GoodsInfoVO;
import com.ruiya.staging.goods.cas.service.GoodsInfoService;
import com.ruiya.staging.goods.cas.util.ResponseUtils;
import com.ruiya.staging.goods.core.constants.ErrorCodeEnum;
import com.ruiya.staging.goods.core.model.GoodsIdVO;
import com.ruiya.staging.goods.core.model.RestResponse;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

@RestController
public class GoodsInfoController {
    private static final Logger _log = LoggerFactory.getLogger(GoodsInfoController.class);

    @Autowired
    private GoodsInfoService goodsInfoService;

    @PostMapping("/goodsinfo/{entId}")
    public RestResponse<GoodsIdVO> createGoodsInfo(@PathVariable("entId") String entId, @RequestBody @Valid GoodsInfoVO goodsInfo, BindingResult bindingResult) {
        String tid = UUID.randomUUID().toString();
        if(bindingResult.hasErrors()){
            Pair<String, String> err = ResponseUtils.getParamVerifyError((bindingResult));
            return new RestResponse<>(err.getKey(), err.getValue(), tid);
        }

        Pair<ErrorCodeEnum, GoodsIdVO> result = goodsInfoService.createGoodsInfo(tid, goodsInfo);
        if(result.getKey().isFail()){
            return new RestResponse<>(result.getKey(), tid);
        }

        GoodsIdVO goodsIdVO = result.getValue();
        return new RestResponse<>(goodsIdVO);
    }

    @GetMapping("/goodsinfo/{entId}/{goodsId}")
    public RestResponse<GoodsInfoVO> getGoodsInfo(@PathVariable("entId") String entId, @PathVariable("goodsId") String goodsId){
        String tid = UUID.randomUUID().toString();
        if(StringUtils.isEmpty(entId)){
            return new RestResponse<>(ErrorCodeEnum.PARAMSERROR.getCode(), "getGoodsInfo entId不能为空", tid);
        }
        if(StringUtils.isEmpty(goodsId)){
            return new RestResponse<>(ErrorCodeEnum.PARAMSERROR.getCode(), "getGoodsInfo goodsNo不能为空", tid);
        }

        Pair<ErrorCodeEnum, GoodsInfoVO> result = this.goodsInfoService.getGoodsInfo(tid, entId, goodsId);
        if(result.getKey().isFail()){
            return new RestResponse<>(result.getKey(), tid);
        }
        GoodsInfoVO goodsInfoVO = result.getValue();

        return new RestResponse<>(goodsInfoVO);
    }

    @GetMapping("/goodsinfo/{entId}/goodsno/{goodsNo}")
    public RestResponse<GoodsIdVO> getGoodsInfoByGoodsNo(@PathVariable("entId") String entId, @PathVariable("goodsNo") String goodsNo){
        String tid = UUID.randomUUID().toString();
        if(StringUtils.isEmpty(entId)){
            return new RestResponse<>(ErrorCodeEnum.PARAMSERROR.getCode(), "getGoodsInfoByGoodsNo entId不能为空", tid);
        }
        if(StringUtils.isEmpty(goodsNo)){
            return new RestResponse<>(ErrorCodeEnum.PARAMSERROR.getCode(), "getGoodsInfoByGoodsNo goodsNo不能为空", tid);
        }

        Pair<ErrorCodeEnum, GoodsIdVO> result = this.goodsInfoService.getGoodsId(tid, entId, goodsNo);
        if(result.getKey().isFail()){
            return new RestResponse<>(result.getKey(), tid);
        }
        GoodsIdVO goodsIdVO = result.getValue();

        return new RestResponse<>(goodsIdVO);
    }

}
